package com.woniu.smart.service.impl;

import com.woniu.smart.dto.EnterpricesDTO;
import com.woniu.smart.entity.Contract;
import com.woniu.smart.entity.Enterprise;
import com.woniu.smart.mapper.ContractMapper;
import com.woniu.smart.mapper.EnterpriseMapper;
import com.woniu.smart.mapper.StaffsMapper;
import com.woniu.smart.service.EnterpriseService;
import com.woniu.smart.vo.EChartsEnterpriseVO;
import com.woniu.smart.vo.EnterpriseVo;
import com.woniu.smart.vo.SelByNameEnterpriseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @Description: 对企业表crud
 * @Author: MeiBing
 * @Date: 2022/10/20 14:00
 */
@Service
public class EnterpriseServiceImpl implements EnterpriseService {

    @Autowired
    EnterpriseMapper enterpriseMapper;

    @Autowired
    StaffsMapper staffsMapper;

    @Autowired
    ContractMapper contractMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return enterpriseMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteAllByList(List item){
        return enterpriseMapper.deleteAllByList(item);
    };


    @Override
    public int insert(Enterprise record) {
        return 0;
    }

    /**
     * 新增企业表信息
     * @param record
     * @return
     */
    @Override
    public int insertSelective(Enterprise record) {
        return enterpriseMapper.insertSelective(record);
    }

    /***
     * @param record
    * @return int
    * @author meibing
    * @Descrip 新增企业对象同时绑定合同对象，并新增企业信息表。
    */
    @Transactional
    @Override
    public  int insertSelectives(EnterpricesDTO record){
        //新增企业表，获得企业id
        enterpriseMapper.insertSelectiveByEnterpricesDTO(record);

        //将员工类型设置为负责人
        record.setStaffstypeId(1);
        //根据企业id  新增企业员工即企业负责人。
        staffsMapper.insertSelectives(record);

        //将企业负责人id更新至企业表中，
        Enterprise enterprise = new Enterprise();
        enterprise.setEnterpriseId(record.getEnterpriseId());
        enterprise.setStaffsId(record.getStaffsId());
        enterpriseMapper.updateByPrimaryKeySelective(enterprise);

        //同时将企业负责人id更新至合同负责人id处
        Contract contract = new Contract();
        contract.setContractId(record.getContractId());
        contract.setStaffsId(record.getStaffsId());
        contractMapper.updateByPrimaryKeySelective(contract);

        return record.getEnterpriseId();
    };
    /**
     * 根据id查询企业表
     * @param id
     * @return
     */
    @Override
    public Enterprise selectByPrimaryKey(Long id) {
        return enterpriseMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询全部企业信息
     * @param
     * @return
     */
    @Override
    public List<EnterpriseVo> selectEnterpriseAll(EnterpricesDTO enterpricesDTO) {
        return enterpriseMapper.selectEnterpriseAllS(enterpricesDTO);
    }

    /**
     * @Description 企业员工查询企业信息
     * @Para a
     * @Retun a
     * @Author WangMr
     * @Date Create by 2022/11/3 16:20
     */
    @Override
    public List<EnterpriseVo> selectEnterpriseAndStaffsAll(EnterpricesDTO enterpricesDTO) {
        return enterpriseMapper.selectEnterpriseAndStaffsAll(enterpricesDTO);
    }

    @Override
    public SelByNameEnterpriseVo selByNameEnterprise(EnterpricesDTO enterpricesDTO) {
        return enterpriseMapper.selByNameEnterprise(enterpricesDTO);
    }

    /**
     * 模糊查询根据enterpriseVo的属性值
     * @param enterpriseVo
     * @return List<Enterprise>
     */
    @Override
    public  List<Enterprise> selectEnterpriseByEnterpriseVo(EnterpriseVo enterpriseVo){
        return enterpriseMapper.selectEnterpriseByEnterpriseVo(enterpriseVo);
    };



    @Override
    public int updateByPrimaryKeySelective(Enterprise record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Enterprise record) {
        return 0;
    }

    /**
     * @Description 公司分组查自己员工数据
     * @Parm a
     * @Retrn a
     * @Author WangMr
     * @Date Create by 2022/10/31 19:43
     */
    @Override
    public List<EChartsEnterpriseVO> selectGroupEnterprise() {
        return enterpriseMapper.selectGroupEnterprise();
    }
//    公司分组
    @Override
    public List<EChartsEnterpriseVO> selectGroupEnterprise2() {
        return enterpriseMapper.selectGroupEnterprise2();
    }
}
